package cn.edu.nuc.bre.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import cn.edu.nuc.bre.dto.UserInfo;

/**
 * 登录拦截器
 * @author 王凯
 *
 */
public class LoginInterceptor extends AbstractInterceptor {

	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 拦截器的拦截规则
	 */
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		//1.得到ActionContext
		ActionContext ctx = invocation.getInvocationContext();
		
		//2.取得用户的登录信息
		UserInfo userInfo = (UserInfo) ctx.getSession().get("userInfo");
		
		//3.判断用户是否登录
		if (userInfo != null && userInfo.getUser().getFirstName() != null) {
			return invocation.invoke();
		} 
		//用户未登录，回到登陆页面
		return Action.LOGIN;
	}

}
